package com.chrilwe.pigpig.filesystem.controller;

import java.io.IOException;

import org.csource.common.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.chrilwe.pigpig.filesystem.common.ResultEnum;
import com.chrilwe.pigpig.filesystem.common.response.ImageUploadResult;
import com.chrilwe.pigpig.filesystem.service.ImageFileService;

import cn.chrilwe.pigpig.core.base.BaseController;
import cn.chrilwe.pigpig.core.response.BaseResult;
import cn.chrilwe.pigpig.core.util.HttpServletRequestResolve;

/**
 * 
 * @author chrilwe
 * @date 2020-9-11
  * 图片文件管理
 */
@RestController
@RequestMapping("/file/image")
public class ImageFileController extends BaseController {
	
	@Autowired
	protected ImageFileService imageFileService;

	@PostMapping("/upload")
	public ImageUploadResult uploadImage(@RequestBody MultipartFile image) {
		HttpServletRequestResolve resolve = new HttpServletRequestResolve(request);
		try {
			String imageUrl = imageFileService.uploadImage(image, resolve.getUserId());
			return new ImageUploadResult(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getName(), imageUrl);
		} catch (Exception e) {
			e.printStackTrace();	
		} 
		return new ImageUploadResult(ResultEnum.UNKNOW_ERR.getCode(), ResultEnum.UNKNOW_ERR.getName(), "");
	}
	
	@GetMapping("/delete")
	public BaseResult deleteImage(@RequestParam("imageId")int imageId) {
		
		return null;
	}
}
